//冒泡排序
function bubbleSort(arr) {
  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = 0; j < arr.length - 1 - i; j++) {
      if (arr[j] > arr[j + 1]) {
        var temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
}

//选择排序
function selectSort(arr) {
  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[i] > arr[j]) {
        var temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
    }
  }
}

//随机生成n位验证码
function creatCode(n) {
  var arr = [];
  for (let i = 0; i < n; i++) {
    var num = Math.floor(Math.random() * 123);
    if (num >= 0 && num <= 9) {
      arr.push(num);
    } else if ((num >= 65 && num <= 90) || (num >= 97 && num <= 122)) {
      arr.push(String.fromCharCode(num));
    } else {
      i--;
    }
  }
  return arr.join("");
}

function $(id) {
  return typeof id === "string" ? document.getElementById(id) : null;
}
//跨浏览器兼容
function getStyle(node, cssStyle) {
  return node.currentStyle
    ? node.currentStyle[cssStyle]
    : getComputedStyle(node)[cssStyle];
}

function drag(node) {
  node.onmousedown = function (ev) {
    var e = ev || window.event; //考虑浏览器兼容问题
    var offectX = e.clientX - node.offsetLeft;
    var offectY = e.clientY - node.offsetTop;

    document.onmousemove = function (ev) {
      var e = ev || window.event;
      var left = e.clientX - offectX;
      var top = e.clientY - offectY;
      node.style.left = left + "px";
      node.style.top = top + "px";
    };
  };
  //取消拖拽
  document.onmouseup = function () {
    document.onmousemove = null;
  };
}
function limitDrag(node) {
  node.onmousedown = function (ev) {
    var e = ev || window.event; //考虑浏览器兼容问题
    var offectX = e.clientX - node.offsetLeft;
    var offectY = e.clientY - node.offsetTop;

    document.onmousemove = function (ev) {
      var e = ev || window.event;
      var left = e.clientX - offectX;
      var top = e.clientY - offectY;
      var windowWidth =
        document.documentElement.clientWidth || document.body.clientWidth; //获取串口宽度
      if (left <= 0) {
        left = 0;
      }
      if (left >= windowWidth - node.offsetWidth) {
        left = windowWidth - node.offsetWidth;
      }
      if (top <= 0) {
        top = 0;
      }
      var windowHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      if (top >= windowHeight - node.offsetHeight) {
        top = windowHeight - node.offsetHeight;
      }
      node.style.left = left + "px";
      node.style.top = top + "px";
    };
  };
  //取消拖拽
  document.onmouseup = function () {
    document.onmousemove = null;
  };
}
//面向对象拖拽实现
function Drag(id) {
  this.oDiv1 = document.getElementById(id);
  var _this = this;
  this.oDiv1.onmousedown = function (ev) {
    _this.funDown(ev);
  };
  //取消拖拽
  document.onmouseup = this.funUp;
}
Drag.prototype.funMover = function (ev) {
  var e = ev || window.event;
  var left = e.clientX - this.offectX;
  var top = e.clientY - this.offectY;
  this.oDiv1.style.left = left + "px";
  this.oDiv1.style.top = top + "px";
};
Drag.prototype.funUp = function () {
  document.onmousemove = null;
};
Drag.prototype.funDown = function (ev) {
  var e = ev || window.event; //考虑浏览器兼容问题
  this.offectX = e.clientX - this.oDiv1.offsetLeft;
  this.offectY = e.clientY - this.oDiv1.offsetTop;
  var _this = this;
  document.onmousemove = function (ev) {
    _this.funMover(ev);
  };
};
function LimitDrag(id) {
  //继承父一级的所有属性
  Drag.apply(this, [id]);
}
//继承方法 原型链
for (var funName in Drag.prototype) {
  LimitDrag.prototype[funName] = Drag.prototype[funName];
}
LimitDrag.prototype.funMover = function (ev) {
  var e = ev || window.event;
  var left = e.clientX - this.offectX;
  var top = e.clientY - this.offectY;
  if (left <= 0) {
    left = 0;
  }
  var windowWidth =
    document.documentElement.clientWidth || document.body.clientWidth; //获取窗口宽度
  if (left >= windowWidth - this.oDiv1.offsetWidth) {
    left = windowWidth - this.oDiv1.offsetWidthh;
  }
  if (top <= 0) {
    top = 0;
  }
  var windowHeight =
    document.documentElement.clientHeight || document.body.clientHeight;
  if (top >= windowHeight - this.oDiv1.offsetHeight) {
    top = windowHeight - this.oDiv1.offsetHeight;
  }
  this.oDiv1.style.left = left + "px";
  this.oDiv1.style.top = top + "px";
};
